<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 */
class AppController extends Controller {
	
	var $helpers = array('Html', 'Javascript', 'Session', 'Ajax');

	function beforeFilter()
	{
		$this->checkAccess();
		$this->params["currentUserId"] = $this->getCurrentUserId();
	}
//	function beforeRender()
//	{
//		$this->layout = 'main';
//	}
	
	// custom functions helper
	
	function ap($array)
	{
		echo "<pre>";
		print_r($array);
		echo "</pre>";
		exit;
	}
	
	function convertDateToSql($date)
	{
		if($date == "")
		{
			return $date;
		}

		$date = explode("/", $date);
		return $date[2] . "-" . $date[1] . "-" . $date[0]; 
	}
	
	function convertDateFromSql($date)
	{
		if($date == "")
		{
			return $date;
		}

		$date = explode("-", $date);
		return $date[2] . "/" . $date[1] . "/" . $date[0]; 
	}
	
	function checkAccess()
	{
		$user = $this->Session->read('User');
		$controller = $this->params['controller'];
		$action = $this->params['action'];
		
                $this->log($user);
		if(($user == null) && !($this->params['controller'] == "users"))
		{
			$this->redirect("/users/login");
		}
                //$this->log($this->params);  
                //$this->redirect("/Masters/index");
    }
	
	function getCurrentUserId()
	{    
		$user = $this->Session->read('User');
		
		if($user != null)
		{		
			return $user['id'];
		}
		else
		{
			return 0;
		}
    }
}
